package JDBC;

import java.sql.*;

public class jdbc_study3 {
    public static void main(String[] args) {
        Statement stmt = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String sql = "select * from account";
            conn = DriverManager.getConnection("jdbc:mysql:///db", "root", "root");
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            //让光标下移一行
            while (rs.next()) {
                //获取数据
                int id = rs.getInt(1);
                //用数字代表的是列的编号
                //mysql的数据是从1开始的
                String name = rs.getString("name");
                //用名字是代表列的名称
                double balance = rs.getDouble(3);

                System.out.println(id + "===" + name + "===" + balance);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println(e);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
